package com.gable.exhibition.service.impl;

import com.gable.common.util.*;
import com.gable.exhibition.dao.MaintainerDao;
import com.gable.exhibition.dao.ManufacturerMaintainerDao;
import com.gable.exhibition.dao.UserDao;
import com.gable.exhibition.init.MaintainerManufacturerInit;
import com.gable.exhibition.init.ManufacturerInit;
import com.gable.exhibition.model.Maintainer;
import com.gable.exhibition.model.ManufacturerMaintainer;
import com.gable.exhibition.model.User;
import com.gable.exhibition.service.MaintainerService;
import com.gable.exhibition.util.MessageUtil;
import com.gable.exhibition.view.ImportExcelMaintainerModel;
import com.gable.exhibition.view.MaintainerView;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Lesson on 2018/2/9.
 */
@Service("maintainerService")
public class MaintainerServiceImpl implements MaintainerService {

    Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    MaintainerDao maintainerDao;

    @Autowired
    ManufacturerMaintainerDao manufacturerMaintainerDao;

    @Autowired
    UserDao userDao;

    @Autowired
    MaintainerManufacturerInit maintainerManufacturerInit;

    @Autowired
    ManufacturerInit manufacturerInit;

    @Override
    public ReturnResultUtil queryMaintainerList(MaintainerView maintainerView) {
        int count = maintainerDao.queryMaintainerCount(maintainerView);
        List<MaintainerView> list = maintainerDao.queryMaintainerList(maintainerView);
        MaintainerView maintainerView1 = new MaintainerView();
        maintainerView1.setCount(count);
        maintainerView1.setList(list);
        return new ReturnResultUtil(200, MessageUtil.QUERYSUCCESS,maintainerView1);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ReturnResultUtil addOrUpdateMaintainer(MaintainerView maintainerView) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        maintainerView.setManufacturer(false);
        if(!StringUtil.isEmpty(maintainerView.getMaintainerNo())){
            int count = maintainerDao.checkMaintainerNo(maintainerView);
            if(count > 0){
                return new ReturnResultUtil(500,MessageUtil.MAINDOESTEXIST);
            }
        }
        int result = 0;
        if(maintainerView.getMaintainerId() == null){
            maintainerView.setMaintainerId(UUIDGenerator.getUniqueLong());
            result = maintainerDao.insertSelective(maintainerView);
            if(result>0){
                User users = new User();
                users.setUserId(UUIDGenerator.getUniqueLong());
                users.setTag(1005L);
                users.setType(1103L);
                String userName = ChineseToEnglish.getPinYinHeadChar(maintainerView.getMaintainerName())+"_admin";
                users.setUserName(userName);
                String password = MD5Util.md5("123456");
                users.setPassword(password);
                users.setRealName("维修商管理员");
                users.setMaintainerId(maintainerView.getMaintainerId());
                users.setRemark("默认生成维修商管理员");
                users.setCreateTime(df.format(new Date()));
                users.setCreatePerson(maintainerView.getCreatePerson());
                users.setIsDel(0);
                userDao.insertSelective(users);
            }
        }else{
            result = maintainerDao.updateByPrimaryKeySelective(maintainerView);
        }
        if(result > 0){
            return new ReturnResultUtil(200,MessageUtil.SAVESUCCESS);
        }
        return new ReturnResultUtil(500,MessageUtil.SAVEFAIL);
    }

    @Override
    public ReturnResultUtil queryMaintainerInfo(Long maintainerId) {
        MaintainerView maintainerView = maintainerDao.queryMaintainerInfo(maintainerId);
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,maintainerView);
    }

    @Override
    public ReturnResultUtil saveMaintainerByExcel(List<ImportExcelMaintainerModel> list, User user) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int count = 0;

        for(ImportExcelMaintainerModel iemm : list){
            Maintainer maintainer = new Maintainer();
            maintainer.setMaintainerId(UUIDGenerator.getUniqueLong());
            maintainer.setMaintainerName(iemm.getMaintainerName());
            maintainer.setCreatePerson(user.getUserId());
            maintainer.setIsDel(0);
            maintainer.setAddress(iemm.getAddress());
            maintainer.setAreaId(iemm.getAreaId());
            maintainer.setCityId(iemm.getCityId());
            maintainer.setCreateTime(df.format(new Date()));
            maintainer.setCreditCode(iemm.getCreditCode());
            maintainer.setMaintainerContacts(iemm.getMaintainerContacts());
            maintainer.setMaintainerPhone(iemm.getMaintainerPhone());
            maintainer.setProvinceId(iemm.getProvinceId());
            maintainer.setRegistrationNumber(iemm.getRegistrationNumber());
            maintainer.setRemark(iemm.getRemark());
            maintainer.setServerPhone(iemm.getServerPhone());
            maintainer.setMaintainerNo(iemm.getMaintainerNo());
            maintainer.setManufacturer(false);
            maintainer.setShortName(iemm.getShortName());
            int result = maintainerDao.insertSelective(maintainer);
            if(result > 0){
                count++;
                if(iemm.getManufacturerId() != null){
                    ManufacturerMaintainer manufacturerMaintainer = new ManufacturerMaintainer();
                    manufacturerMaintainer.setManufacturerMaintainerId(UUIDGenerator.getUniqueLong());
                    manufacturerMaintainer.setMaintainerId(maintainer.getMaintainerId());
                    manufacturerMaintainer.setManufacturerId(iemm.getManufacturerId());
                    manufacturerMaintainerDao.insertSelective(manufacturerMaintainer);
                }

                User users = new User();
                users.setUserId(UUIDGenerator.getUniqueLong());
                users.setTag(1005L);
                users.setType(1013L);
                String userName = ChineseToEnglish.getPinYinHeadChar(maintainer.getMaintainerName())+"_admin";
                users.setUserName(userName);
                String password = MD5Util.md5("123456");
                users.setPassword(password);
                users.setRealName("维修商管理员");
                users.setMaintainerId(maintainer.getMaintainerId());
                users.setRemark("默认生成维修商管理员");
                users.setCreateTime(df.format(new Date()));
                users.setCreatePerson(maintainer.getCreatePerson());
                users.setIsDel(0);
                userDao.insertSelective(users);
            }
        }
        if(count != list.size()){
            ManufacturerMaintainer manufacturerMaintainer=null;
            manufacturerMaintainer.getManufacturerId();
        }

        return new ReturnResultUtil(200,MessageUtil.SAVESUCCESS);
    }

    @Override
    public ReturnResultUtil queryMaintainerListByManuId(Long manufacturerId) {
        List<MaintainerView> list = maintainerDao.queryMaintainerListByManuId(manufacturerId);
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,list);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ReturnResultUtil addMaintainerByManuId(MaintainerView maintainerView) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        int count = maintainerDao.checkMaintainerNo(maintainerView);
        if(count > 0){
            return new ReturnResultUtil(500,MessageUtil.MAINDOESTEXIST);
        }
        maintainerView.setMaintainerId(UUIDGenerator.getUniqueLong());
        maintainerView.setManufacturer(false);
        int result = maintainerDao.insertSelective(maintainerView);
        if(result > 0){
                //插入厂商和维修商之间的关系
                ManufacturerMaintainer manufacturerMaintainer = new ManufacturerMaintainer();
                manufacturerMaintainer.setManufacturerId(maintainerView.getManufacturerId());
                manufacturerMaintainer.setMaintainerId(maintainerView.getMaintainerId());
                manufacturerMaintainer.setManufacturerMaintainerId(UUIDGenerator.getUniqueLong());
                manufacturerMaintainerDao.insertSelective(manufacturerMaintainer);


                //默认生成维修商管理员账号
                User users = new User();
                users.setUserId(UUIDGenerator.getUniqueLong());
                users.setTag(1005L);
                users.setType(1013L);
                String userName = ChineseToEnglish.getPinYinHeadChar(maintainerView.getMaintainerName())+"_admin";
                users.setUserName(userName);
                String password = MD5Util.md5("123456");
                users.setPassword(password);
                users.setRealName("维修商管理员");
                users.setMobile(maintainerView.getMaintainerPhone());
                users.setMaintainerId(maintainerView.getMaintainerId());
                users.setRemark("默认生成维修商管理员");
                users.setCreateTime(df.format(new Date()));
                users.setCreatePerson(maintainerView.getCreatePerson());
                users.setIsDel(0);
                userDao.insertSelective(users);
        }

        return new ReturnResultUtil(200,MessageUtil.SAVESUCCESS);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ReturnResultUtil detainMaintainerByManuId(Maintainer maintainer) {
        int result = maintainerDao.updateByPrimaryKeySelective(maintainer);
        if(result>0){
            return new ReturnResultUtil(200,MessageUtil.SAVESUCCESS);
        }
        return new ReturnResultUtil(500,MessageUtil.SAVEFAIL);
    }

    @Override
    public ReturnResultUtil queryMaintainerByName(MaintainerView maintainerView) {
        List<Maintainer> maintainers = maintainerDao.queryMaintainerByName(maintainerView);
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,maintainers);
    }

    @Override
    @Transactional
    public Long checkMaintainer(Maintainer maintainer) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Maintainer maintainer1 = maintainerDao.checkMaintainer(maintainer);
        if(maintainer1 != null){
            return maintainer1.getMaintainerId();
        }

        maintainer.setIsDel(0);
        maintainer.setMaintainerId(UUIDGenerator.getUniqueLong());
        maintainer.setCreateTime(df.format(new Date()));
        int reuslt = maintainerDao.insertSelective(maintainer);
        if(reuslt > 0){
            maintainerManufacturerInit.queryMaintainerManufacturer();
            manufacturerInit.queryManufacturerMaintainer();
            return maintainer.getMaintainerId();
        }
        return null;
    }
}
